/*
Replication file for "The Policy Effects of the Partisan Composition of State Government"
Authors: Devin Caughey, Chris Warshaw, Yiqing Xu

Software: STATA 14
*/


ssc install reghdfe /* for estimating fixed effects models */
ssc install outreg2 /* for exporting results */


clear all
set more off

**Set the working directory in Stata to whatever directory you downloaded the replication materials from our paper.

**cd "~/Dropbox/Projects/PartyControl_StateLegislatures/Replication_Archive/"

************************
*  Table 2: baseline    
************************

use "party_control_data_161121_stata.dta", clear

// generating varialbes
g gov_dem = gov_party == 1
g gov_dem_X_hs_dem = gov_dem * hs_dem_control
g gov_dem_X_sen_dem = gov_dem * sen_dem_control
g both_dem = hs_dem_control * sen_dem_control
g full_dem = gov_dem * hs_dem_control * sen_dem_control
egen state = group(abb)

// clearning up
cap erase Table2.txt
cap erase Table2.tex

// Column 1
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy gov_dem hs_dem_control sen_dem_control, a(state year) 
outreg2 using Table2, tex dec(3) nocons noas addstat(States, e(N_clust))

// Column 2
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy gov_dem hs_dem_control sen_dem_control Policy_L1 Policy_L2, a(state year) 
outreg2 using Table2, tex dec(3) nocons noas addstat(States, e(N_clust))

// Column 3
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy gov_dem hs_dem_control sen_dem_control both_dem gov_dem_X_hs_dem gov_dem_X_sen_dem full_dem Policy_L1 Policy_L2 , a(state year)
outreg2 using Table2, tex dec(3) nocons noas addstat(States, e(N_clust))


// Column 4
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy gov_dem hs_dem_control sen_dem_control Policy_L1 Policy_L2 if South11 == 0, a(state year) 
outreg2 using Table2, tex dec(3) nocons noas addstat(States, e(N_clust))

// Column 5
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy gov_dem hs_dem_control sen_dem_control Policy_L1 Policy_L2 if South11 == 1, a(state year) 
outreg2 using Table2, tex dec(3) nocons noas addstat(States, e(N_clust))

/* visualize -- Appendix */
reghdfe Policy gov_dem hs_dem_control sen_dem_control both_dem gov_dem_X_hs_dem gov_dem_X_sen_dem full_dem Policy_L1 Policy_L2 , a(state year) cl(state)
mat AA = J(7,2,.)
lincom hs_dem_control
mat AA[1,1]=r(estimate)
mat AA[1,2]=r(se)
lincom sen_dem_control
mat AA[2,1]=r(estimate)
mat AA[2,2]=r(se)
lincom hs_dem_control + sen_dem_control + both_dem
mat AA[3,1]=r(estimate)
mat AA[3,2]=r(se)
lincom gov_dem
mat AA[4,1]=r(estimate)
mat AA[4,2]=r(se)  
lincom gov_dem + hs_dem_control + gov_dem_X_hs_dem
mat AA[5,1]=r(estimate)
mat AA[5,2]=r(se)
lincom gov_dem + sen_dem_control + gov_dem_X_sen_dem
mat AA[6,1]=r(estimate)
mat AA[6,2]=r(se)
lincom gov_dem + hs_dem_control + sen_dem_control + both_dem + gov_dem_X_hs_dem + gov_dem_X_sen_dem + full_dem
mat AA[7,1]=r(estimate)
mat AA[7,2]=r(se)
mat list AA
svmat AA
keep AA*
keep if AA1~=.
ren AA1 coef
ren AA2 se
g CI1 = coef - se*1.96
g CI2 = coef + se*1.96
save fg_baseline,  replace

**************************************
*  Table 3: Mass and Elite Divergence  
**************************************

use "party_control_data_161121_stata.dta", clear

// generating varialbes
g gov_dem = gov_party == 1
g gov_dem_X_hs_dem = gov_dem * hs_dem_control
g gov_dem_X_sen_dem = gov_dem * sen_dem_control
g both_dem = hs_dem_control * sen_dem_control
g full_dem = gov_dem * hs_dem_control * sen_dem_control
egen state = group(abb)

g yr69_91 = inrange(year, 1969, 1991)
g yr92_14 = inrange(year, 1992, 2014)
g DemControl_69_91 = DemControl * yr69_91
g DemControl_92_14 = DemControl * yr92_14
sum MassPartisanDivergence_L1 if year >= 1994
g MassDv = MassPartisanDivergence_L1/r(sd) /* standardize based on stats of the last period */
sum StateLegDivergence_L1 if year >= 1994
g EliteDv = StateLegDivergence_L1 /r(sd)  /* standardize */
g DemControl_MassDv = DemControl * MassDv
g DemControl_EliteDv = DemControl * EliteDv

// clearning up
cap erase Table3.txt
cap erase Table3.tex

// Column 1 (1936-2014)
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy DemControl DemControl_69_91 DemControl_92_14 Policy_L1 Policy_L2, a(year state#Era3) 
outreg2 using Table3, tex dec(3) nocons noas addstat(States, e(N_clust))

// Column 2 (1947-2014)
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy DemControl DemControl_69_91 DemControl_92_14 Policy_L1 Policy_L2 if year>=1947, a(year state#Era3) 
outreg2 using Table3, tex dec(3) nocons noas addstat(States, e(N_clust))

// Column 3 (1947-2014)
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy DemControl DemControl_69_91 DemControl_92_14 MassDv DemControl_MassDv Policy_L1 Policy_L2, a(year state#Era3) 
outreg2 using Table3, tex dec(3) nocons noas addstat(States, e(N_clust))

// Column 4 (only for the period of 1993-2014)
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy DemControl EliteDv DemControl_EliteDv  Policy_L1 Policy_L2, a(state year) 
outreg2 using Table3, tex dec(3) nocons noas addstat(States, e(N_clust))

// Column 5 (only for the period of 1993-2014)
bootstrap _b, reps(1000) cluster(state) seed(01239): reghdfe Policy DemControl MassDv DemControl_MassDv EliteDv DemControl_EliteDv  Policy_L1 Policy_L2, a(state year) 
outreg2 using Table3, tex dec(3) nocons noas addstat(States, e(N_clust))


/* Check if interactions are valid */
/*
reghdfe Policy Policy_L1 Policy_L2, a(state year state#Era3, save)
predict res_Policy, res

reghdfe DemControl Policy_L1 Policy_L2, a(state year state#Era3, save)
predict res_DemControl, res

reghdfe MassDv Policy_L1 Policy_L2, a(state year state#Era3, save)
predict res_MassDv, res

reghdfe EliteDv Policy_L1 Policy_L2, a(state year state#Era3, save)
predict res_EliteDv, res

keep state year res*
save fg_inter.dta replace  // check in R
*/
